package com.example.iconmaker;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;
import android.view.VelocityTracker;

public class OpenGLRenderer implements Renderer {

	public static final int MIN_TOUCH_MOVE = 25;
	public static final int RIGHT_WHEEL = 0;
	public static final int LEFT_WHEEL = 1;
	public static final int HORIZONTAL_BAR = 2;
	public static final int OPERATE_BAR = 3;
	
	private int mWidth;
	private int mHeight;
	public static final float ORTHO = 10.0f;
	
	private Bar mHorizontal;
	private Wheel mRightWheel;
	private Wheel mLeftWheel;
	private float mLastAngle = 0;
	private float mLastY;
	private int mWeight;
	private VelocityTracker mVelocityTracker = VelocityTracker.obtain();

	public OpenGLRenderer() {

		mRightWheel = new Wheel(Wheel.RIGHT_SIDE, 10.0f);
		mRightWheel.centreX = 10.0f;
		mRightWheel.contreY = 0.0f;
		

		mLeftWheel = new Wheel(Wheel.LEFT_SIDE, 10.0f);
		mLeftWheel.centreX = -10.0f;
		mLeftWheel.contreY = 0.0f;
		
		mHorizontal = new Bar();
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		// gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		//以下两句会使Png图片背景变为透明
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_BLEND);

		mRightWheel.initTexture(GLImage.bgs, gl);
		mLeftWheel.initTexture(GLImage.masks, gl);
		mHorizontal.initTexture(GLImage.icons, gl);
	}

	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -9.0f);
		mRightWheel.draw(gl);
		
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -8.0f);
		mHorizontal.draw(gl);

		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, 0.0f, -7.0f);
		mLeftWheel.draw(gl);
		
		
		gl.glFinish();
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {

		mWidth = width;
		mHeight = height;
		float ratio = (float) height / width;

		// Sets the current view port to the new size.

		gl.glViewport(0, 0, width, height);

		// Select the projection matrix

		gl.glMatrixMode(GL10.GL_PROJECTION);

		// Reset the projection matrix

		gl.glLoadIdentity();

		// Calculate the aspect ratio of the window

		// gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		gl.glOrthof(-ORTHO, ORTHO, -ORTHO * ratio, ORTHO * ratio, 1, ORTHO);

		// Select the modelview matrix

		gl.glMatrixMode(GL10.GL_MODELVIEW);

		// Reset the modelview matrix

		gl.glLoadIdentity();
	}

	public void onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		final int action = event.getAction() & MotionEvent.ACTION_MASK;
		mVelocityTracker.addMovement(event);
		mVelocityTracker.computeCurrentVelocity(1);
		

		float change = 0;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mLastY = y;
			if (x < mWidth / 2) {
				mWeight = LEFT_WHEEL;
				mLastAngle = mLeftWheel.mAngle;
				mLeftWheel.mSpeed = 0;
			} else {
				mWeight = RIGHT_WHEEL;
				mLastAngle = mRightWheel.mAngle;
				mRightWheel.mSpeed = 0;
			}

			break;
		case MotionEvent.ACTION_MOVE:

			
			if (y > MIN_TOUCH_MOVE) {
				change = getChagneAngle(y);
			}
			switch (mWeight) {
			case LEFT_WHEEL:
				mLeftWheel.setAngle(mLastAngle+change);
				break;
			case RIGHT_WHEEL:
				mRightWheel.setAngle(mLastAngle+change);
				break;
			default:
				break;
			}
			break;
		case MotionEvent.ACTION_UP:
			float yVelocit =mVelocityTracker.getYVelocity();
			switch (mWeight) {
			case LEFT_WHEEL:
				if(yVelocit == 0){
					mLeftWheel.align();
				}else{
					mLeftWheel.mSpeed = -yVelocit;
				}
				break;
			case RIGHT_WHEEL:
				if(yVelocit == 0){
					mRightWheel.align();
				}else{
					mRightWheel.mSpeed = -yVelocit;
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	private float getChagneAngle(float newY) {
		//须注意到android view和opengl中的Y坐标方向相反
		float dy = mLastY - newY;
		float rate = dy / mHeight;
		float angle = Wheel.HALF_AHGLE * rate;
		return angle;
	}

}